package com.jlastu.dlxy_androidapp.presenter;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.jlastu.dlxy_androidapp.base.WeakRefHandler;
import com.jlastu.dlxy_androidapp.contract.MineDetailsContract;
import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.User;

import java.util.ArrayList;
import java.util.List;

public class MineDetailsPresenter implements MineDetailsContract.IMineDetailsPresenter {
    private static final int WHAT_GET_LOGIN_USER = 0;
    private MineDetailsContract.IMineDetailsView mMineDetailsView;
    private Context mContext;

    public MineDetailsPresenter(MineDetailsContract.IMineDetailsView mineDetailsView, Context context) {
        mMineDetailsView = mineDetailsView;
        mContext = context;
    }

    @Override
    public void getUser(int id) {
//        List<User> list = new ArrayList<>();
        new Thread(new Runnable() {
            @SuppressLint("Range")
            @Override
            public void run() {
                Uri uri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/user/" + id);
                Log.d("Presenter", "getNewsList: " + id);
                Cursor cursor = mContext.getContentResolver().query(
                        uri, null, null, null);
                try {
                    if (cursor != null && cursor.moveToFirst()) {
                        User user = new User();
                        user.setUserName(cursor.getString(cursor.getColumnIndex("user_name")));
                        user.setUserPassword(cursor.getString(cursor.getColumnIndex("user_password")));
                        user.setUserPhone(cursor.getString(cursor.getColumnIndex("user_phone")));
                        user.setUserAge(cursor.getString(cursor.getColumnIndex("user_age")));
                        user.setData(cursor.getBlob(cursor.getColumnIndex("user_headimg")));
//                        list.add(user);
                        Log.d("", "user查询全部 ====== " + user);

                        Message message = Message.obtain();
                        message.obj = user;
                        message.what = WHAT_GET_LOGIN_USER;
                        mHandler.sendMessage(message);
                    }
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }

            }
        }).start();

    }

    @Override
    public void updateUser(int userid, User user) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 创建用于更新的ContentValues对象
                ContentValues cv = new ContentValues();
                cv.put("user_name", user.getUserName());
                cv.put("user_phone", user.getUserPhone());
                cv.put("user_age", user.getUserAge());
                cv.put("user_headimg", user.getData());
                // 构建包含用户名的URI
                Uri userUri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/user/" + userid);
                // 执行更新操作
                int updateCount = mContext.getContentResolver().update(userUri, cv, null, null);
                // 打印更新结果
                if (updateCount > 0) {
                    Log.d("", "更新的用户 ===== " + updateCount);
                } else {
                    Log.d("", "用户不存在");
                }
            }
        }).start();
    }

    @Override
    public void updateHeadImg(int userid, User user) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 创建用于更新的ContentValues对象
                ContentValues cv = new ContentValues();
                cv.put("user_headimg", user.getData());
                // 构建包含用户名的URI
                Uri userUri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/user/" + userid);
                // 执行更新操作
                int updateCount = mContext.getContentResolver().update(userUri, cv, null, null);
                // 打印更新结果
                if (updateCount > 0) {
                    Log.d("", "更新的用户 ===== " + updateCount);
                } else {
                    Log.d("", "用户不存在");
                }
            }
        }).start();
    }

    @Override
    public void updateUserName(int userid, User user) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 创建用于更新的ContentValues对象
                ContentValues cv = new ContentValues();
                cv.put("user_name", user.getUserName());
                // 构建包含用户名的URI
                Uri userUri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/user/" + userid);
                // 执行更新操作
                int updateCount = mContext.getContentResolver().update(userUri, cv, null, null);
                // 打印更新结果
                if (updateCount > 0) {
                    Log.d("", "更新的用户 ===== " + updateCount);
                } else {
                    Log.d("", "用户不存在");
                }
            }
        }).start();
    }

    @Override
    public void updateUserPhone(int userid, User user) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                // 创建用于更新的ContentValues对象
                ContentValues cv = new ContentValues();
                cv.put("user_phone", user.getUserPhone());
                // 构建包含用户名的URI
                Uri userUri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/user/" + userid);
                // 执行更新操作
                int updateCount = mContext.getContentResolver().update(userUri, cv, null, null);
                // 打印更新结果
                if (updateCount > 0) {
                    Log.d("", "更新的用户 ===== " + updateCount);
                } else {
                    Log.d("", "用户不存在");
                }
            }
        }).start();
    }


    @Override
    public void updateUserAge(int userid, User user) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 创建用于更新的ContentValues对象
                ContentValues cv = new ContentValues();
                cv.put("user_age", user.getUserAge());
                // 构建包含用户名的URI
                Uri userUri = Uri.parse("content://com.jlastu.dlxy_androidapp.provider/user/" + userid);
                // 执行更新操作
                int updateCount = mContext.getContentResolver().update(userUri, cv, null, null);
                // 打印更新结果
                if (updateCount > 0) {
                    Log.d("", "更新的用户 ===== " + updateCount);
                } else {
                    Log.d("", "用户不存在");
                }
            }
        }).start();
    }


    Handler mHandler = new WeakRefHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int what = msg.what;
            switch (what){
                case  WHAT_GET_LOGIN_USER:
                mMineDetailsView.showUser((User) msg.obj);
            }
            return false;
        }
    });

}
